package shiyanshi.qiandao.demo.util;



import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import shiyanshi.qiandao.demo.Entity.Qiandao_Records;

import java.io.*;
import java.util.List;
import java.util.Random;

public class jsontest {
    public static void charu(List<Qiandao_Records> list) throws IOException, JSONException {
        BufferedReader br = null;
        String path = "demo1.json";
        try {
            br = new BufferedReader(new InputStreamReader(jsontest.class.getClassLoader().getResourceAsStream(path)));
            String line = br.readLine();
            StringBuilder sb = new StringBuilder();
            while (line != null) {
                sb.append(line + "\r\n");
                line = br.readLine();
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }


        Random random=new Random();
        Tool tool=new Tool();
        JSONArray jsonArray=new JSONArray();
        File file = new File("C:\\Users\\ZDM\\Desktop\\shi-yan-shi-qian-dao\\src\\main\\resources\\static\\test\\table\\demo1.json");
        FileOutputStream fileOutputStream=new FileOutputStream(file);//实例化FileOutputStream
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");//将字符流转换为字节流
        BufferedWriter bufferedWriter= new BufferedWriter(outputStreamWriter);//创建字符缓冲输出流对象

        for(int i=0;i<=list.size();i++){
            JSONObject jsonObject=new JSONObject();//创建JSONObject对象
            jsonObject.put("id",list.get(i).getStuid());//产生1-100的随机数
            jsonObject.put("username",list.get(i).getClass());//产生18-25的随机数
            jsonObject.put("grade",list.get(i).getStart_time());//产生60-100的随机数
            jsonObject.put("id",list.get(i).getStuid());//产生1-100的随机数
            jsonObject.put("logins",list.get(i).getEnd_time());//产生18-25的随机数
            jsonObject.put("sign",list.get(i).getStart_time());//产生60-100的随机数
            jsonArray.put(jsonObject);//将jsonObject对象旁如jsonarray数组中
        }

        System.out.println(jsonArray);

        String jsonString=jsonArray.toString();//将jsonarray数组转化为字符串
        jsonString = "{\"code\": 0,\"msg\": \"\",\"count\": 1,\"data\": " + jsonString + "}";
        String JsonString=tool.stringToJSON(jsonString);//将jsonarrray字符串格式化
        System.out.println(JsonString);
        bufferedWriter.write(JsonString);//将格式化的jsonarray字符串写入文件
        bufferedWriter.flush();//清空缓冲区，强制输出数据
        bufferedWriter.close();//关闭输出流

    }
}
